<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Services\IndexService;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class SeoSetingController extends Controller
{

	/**
	 * Index interface.
	 *
	 * @param Content $content
	 *
	 * @return Content
	 */
	public function index(Content $content)
	{
		return $content->header('SEO设置')->description('默认SEO设置')->body($this->form());
	}


	/**
	 * Make a form builder.
	 *
	 * @return Form
	 */
	protected function form()
	{
		$seo = Redis::get('seo');
		$seo = json_decode($seo,true);
		if ($seo){
			$defaultSetting = [
				'seo_title'       => $seo['seo_title'],
				'seo_description' => $seo['seo_description'],
				'seo_keyword'     => $seo['seo_keyword'],
			];
		}else{
			$defaultSetting = [
				'seo_title'       => '',
				'seo_description' => '',
				'seo_keyword'     => '',
			];
		}


		$form = new Form($defaultSetting);

		$form->action('seo');

		$form->text('seo_title', 'SEO标题')->required();
		$form->text('seo_description', 'SEO描述')->required();
		$form->text('seo_keyword', 'SEO关键字')->required();

		return $form;
	}

	public function store(Request $request)
	{
		$data = $request->all();

		$data = json_encode($data);
		Redis::set('seo', $data);
		IndexService::indexCache();
		admin_toastr('设置成功！');

		return back();
	}
}
